package algorithm;

/**
 * @description: 二分查找
 * @author: apple
 * @date:  2021/11/17 10:56 下午
 * @version 1.0
 **/
public class BinarySearch {

    public static void main(String[] args) {

        int[] nums = {-12 , -5 , 3 , 7 , 12};
        int n = 7;
        int head = 0; //初始索引
        int end = nums.length - 1; //末尾索引
        boolean flag = false;
        while (head <= end) {

            int middle = (head + end) / 2;
            if (n == nums[middle]) {
                flag = true;
                break;
            } else if(nums[middle] > n) {
                end = middle - 1;
            } else {
                head = middle + 1;
            }

        }

        if (flag) {
            System.out.println("找到了");
        } else {
            System.out.println("没有找到");
        }
    }
}
